<template>
  <div class="commont-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入评论"
      show-word-limit
    />
    <van-button class="post-btn" size="small" @click="onPost">发布</van-button>
  </div>
</template>

<script>
// 评论文章接口
import { addPost } from '@/api/commont'

export default {
  name: 'CommontPost',
  components: {},
  inject: {
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      message: ''
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onPost () {
      this.$toast.loading({
        message: '发布中...',
        forbidClick: true,
        duration: 0 // 默认只显示两秒。0一致显示
      })
      // console.log(1)
      try {
        const { data } = await addPost({
          target: this.target.toString(), // 文章id,防止大数字问题
          content: this.message, // 评论内容
          art_id: this.articleId ? this.articleId.toString() : null
        })
        console.log(data)
        this.$toast.success('发布成功')
        this.message = ''
        this.$emit('post-success', data.data)
      } catch (err) {
        this.$toast('发布评论失败，请稍后再试！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.commont-post{
  display: flex;
  align-items: flex-end;
  .post-field{
    border: 1px solid red;
  }
  .post-btn{
    margin-bottom: 50px;
    background-color: pink;
  }
}
</style>
